import {createStore} from 'vuex'
import Cache from '../cache'

const types = {
  USER: 'USER',
  TOKEN: 'TOKEN'
}
export default createStore({
  state: {
    user: {},
    token: ''
  },
  mutations: {
    [types.USER](state, val) {
      state.user = val
      Cache.save(types.USER, val)
    },
    [types.TOKEN](state, val) {
      state.token = val
    }
  },
  actions: {
    setUser: ({commit}, val) => commit(types.USER, val),
    setToken: ({commit}, val) => commit(types.TOKEN, val)
  },
  modules: {},
  getters: {
    user: state => Cache.get(types.USER, state.user),
    token: state => state.token
  }
})
